package basics.day19.LinkedListDemo;

import java.util.Arrays;
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        // fun_1();
        // fun_2();
        fun_3();
    }

    public static void fun_3() {
        LinkedList<String> stringLinkedList = new LinkedList<String>();
        stringLinkedList.addLast("1");
        stringLinkedList.addLast("2");
        stringLinkedList.addFirst("0");
        String str = stringLinkedList.removeFirst();
        System.out.println(str);
        Object[] strings = stringLinkedList.toArray();
        System.out.println(Arrays.toString(strings));

    }

    public static void fun_2() {
        LinkedList<Integer> integerLinkedList = new LinkedList<Integer>();
        integerLinkedList.add(1);
        integerLinkedList.addFirst(0);
        integerLinkedList.addLast(2);
        Object[] objects = integerLinkedList.toArray();
        System.out.println(Arrays.toString(objects));
        /*
         * isEmpty 判断LinkedList是否为空
         * 如果 LinkedList为空的话 调用getFirst和getLast时候报错：
         *      java.util.NoSuchElementException
         * */
        if (integerLinkedList.isEmpty()) return;
        integerLinkedList.clear();
        // 获取第一个元素
        Integer firstInt = integerLinkedList.getFirst();
        // 获取最后一个元素
        Integer lastInt = integerLinkedList.getLast();
        System.out.println("firstInt：" + firstInt);
        System.out.println("lastInt：" + lastInt);
    }

    public static void fun_1() {
        /*
         * add(E e): 将指定元素添加到此列表的结尾。
         * addLast(E e): 将指定元素添加到此列表的结尾。
         * addFirst(E e): 将指定元素插入此列表的开头。
         * */
        LinkedList<String> stringLinkedList = new LinkedList<String>();
        stringLinkedList.addLast("1");
        stringLinkedList.add("2");
        stringLinkedList.addFirst("first");
        Object[] strings = stringLinkedList.toArray();
        System.out.println(Arrays.toString(strings));
    }
}
